//
//  WorkTabBar.m
//  operativeSide-2.0
//
//  Created by 王春程 on 2016/12/15.
//  Copyright © 2016年 王春程. All rights reserved.
//

#import "WorkTabBar.h"
#import "WrokManSendController.h"
#import "WorkManController.h"
#import "StationController.h"
#import "StationSendController.h"
#import "CenterController.h"
#import "CenterSendController.h"
#import "ScanController.h"
#import "PersonalController.h"

@interface WorkTabBar ()<UITabBarControllerDelegate>
@property (strong, nonatomic)               UIView *tabView;
@end

@implementation WorkTabBar
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self.tabBar setBackgroundImage:[[UIImage alloc] init]];
    [self.tabBar setShadowImage:[[UIImage alloc] init]];
    
    _tabView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 49)];
    _tabView.backgroundColor = RGB(246, 246, 246);
    [self.tabBar addSubview:self.tabView];
    
    [self initButtons];
    
}

- (void)setBaseWorkType:(BASEWORKTYPE)baseWorkType {
    _baseWorkType = baseWorkType;
    
    NSMutableArray *baseArray = [NSMutableArray array];
    switch (self.baseWorkType) {
        case BASEWORKTYPEWORKMAN:
        {
            WorkManController *workMan = [[WorkManController alloc] init];
            UINavigationController *workManNav = [[UINavigationController alloc] initWithRootViewController:workMan];
            [baseArray addObject:workManNav];
            
            WrokManSendController *workManSend = [[WrokManSendController alloc] init];
            UINavigationController *workManSendNav = [[UINavigationController alloc] initWithRootViewController:workManSend];
            [baseArray addObject:workManSendNav];
            
        }
            break;
        case BASEWORKTYPESTATION:
        {
            StationController *station = [[StationController alloc] init];
            UINavigationController *stationNav = [[UINavigationController alloc] initWithRootViewController:station];
            [baseArray addObject:stationNav];
            
            StationSendController *stationSend = [[StationSendController alloc] init];
            UINavigationController *stationSendNav = [[UINavigationController alloc] initWithRootViewController:stationSend];
            [baseArray addObject:stationSendNav];
        }
            break;
        case BASEWORKTYPECENTER:
        {
            
            CenterController *center = [[CenterController alloc] init];
            UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:center];
            [baseArray addObject:centerNav];
            
            CenterSendController *centerSend = [[CenterSendController alloc] init];
            UINavigationController *centerSendNav = [[UINavigationController alloc] initWithRootViewController:centerSend];
            [baseArray addObject:centerSendNav];
            
        }
            break;
        default:
            break;
    }
    
    if(baseArray.count == 2) {
        ScanController *scan = [[ScanController alloc] init];
        UINavigationController *scanNav = [[UINavigationController alloc] initWithRootViewController:scan];
        [baseArray addObject:scanNav];
        
        PersonalController *personal = [[PersonalController alloc] init];
        UINavigationController *personalNav = [[UINavigationController alloc] initWithRootViewController:personal];
        [baseArray addObject:personalNav];
    }
    
    self.viewControllers = baseArray;
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarButton removeFromSuperview];
        }
    }
}

- (void)setTabViewHidden:(BOOL)flag {
    self.tabView.hidden = flag;
}

- (void)initButtons {
    NSArray *nameArr = @[@"收件",@"派件",@"扫一扫",@"我的"];
    NSArray *normalPicNameArr = @[@"rec_normal",@"send_normal",@"scan_normal",@"me_normal"];
    NSArray *selectedPicNameArr = @[@"rec_selected",@"send_selected",@"scan_selected",@"me_selected"];
    for (NSUInteger i = 0; i < nameArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake((VIEW_WIDTH/4)*i, 8, VIEW_WIDTH/4, 19)];
        btn.tag = 100*(i+1);
        [btn setImage:[UIImage imageNamed:normalPicNameArr[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selectedPicNameArr[i]] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabView addSubview:btn];
        
        UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [nameBtn setFrame:CGRectMake((VIEW_WIDTH/4)*i, 27, VIEW_WIDTH/4, 22)];
        nameBtn.tag = 100*(i+1)+1;
        [nameBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        [nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [nameBtn setTitleColor:RGB(253, 213, 47) forState:UIControlStateSelected];
        [nameBtn.titleLabel setFont:[UIFont systemFontOfSize:11.0]];
        [nameBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabView addSubview:nameBtn];
        
        if (i == 0) {
            btn.selected = YES;
            nameBtn.selected = YES;
        }
    }
    self.selectedIndex = 0;
}

#pragma mark - pressAction
- (void)btnAction:(UIButton *)sender {
    sender.selected = YES;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSInteger index;
    if(sender.tag % 100 == 0) {
        btn = [sender.superview viewWithTag:sender.tag + 1];
        index = sender.tag/100 - 1;
    } else {
        btn = [sender.superview viewWithTag:sender.tag - 1];
        index = btn.tag/100 - 1;
    }
    btn.selected = YES;
    self.selectedIndex = index;
    for(UIButton *button in sender.superview.subviews) {
        if(button.tag != sender.tag && button.tag != btn.tag) {
            button.selected = NO;
        }
    }
}

@end
